package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }

    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getRoomInfoById(Long id) {
        RoomVo roomVo = roomMapper.getRoomInfoById(id);
        return roomVo;
    }

    /**
     * 获取房间设备信息
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        // 获取到所有房间的设备（房间+床位）
        List<RoomVo> list = roomMapper.getRoomsWithDeviceByFloorId(floorId);

        // 遍历房间找设备
        for (RoomVo room : list) {
            // 1. 获取房间的设备及其数据
            List<DeviceInfo> roomDevices = room.getDeviceVos(); // 房间设备
            if (!CollectionUtils.isEmpty(roomDevices)) {
                log.info("该房间有设备，在redis中获取设备的数据");
                // 获取每一个房间的设备数据（redis）
                for (DeviceInfo roomDevice : roomDevices) {
                    getData(roomDevice);
                }
            }

            // 2. 获取床位的设备及其数据
            List<BedVo> bedVoList = room.getBedVoList();
            if (!CollectionUtils.isEmpty(bedVoList)) {
                log.info("该床位有设备，在redis中获取设备的数据");
                // 遍历床位
                for (BedVo bed : bedVoList) {
                    List<DeviceInfo> bedDeviceVos = bed.getDeviceVos(); // 床位设备
                    if (CollectionUtils.isEmpty(bedDeviceVos)) {
                        continue;
                    }
                    for (DeviceInfo bedDevice : bedDeviceVos) {
                        getData(bedDevice);
                    }
                }
            }
        }
        return list;
    }


    public void getData(DeviceInfo device) {
        //获取设备的数据
        Object roomData = redisTemplate.opsForHash().get("DEVICE_NEW_DATA", device.getIotId());
        List<DeviceData> deviceData = JSONUtil.toList(roomData.toString(), DeviceData.class);
        //判断是否有数据
        if (CollectionUtils.isEmpty(deviceData)) {
            log.info("没有数据");
        }
        //有数据，存入
        device.setDeviceDataVos(deviceData);
    }
}
